package cn.piggy.gateway.interfaces;

import cn.piggy.gateway.common.dto.FileDto;
import cn.piggy.gateway.dto.FilesDto;
import cn.piggy.gateway.dto.OneFileDto;
import cn.piggy.gateway.dto.ReqDto;
import cn.piggy.gateway.rpc.ITestInterface;
import cn.piggy.gateway.sdk.annotation.ApiProducerClazz;
import cn.piggy.gateway.sdk.annotation.ApiProducerMethod;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.UUID;


@Service(version = "1.0.0")
@ApiProducerClazz(interfaceName = "DUBBO-服务", interfaceVersion = "1.0.0")
public class TestInterface implements ITestInterface {

    @Value("${server.port}")
    private String port;

    @Override
    @ApiProducerMethod(methodType = "DUBBO", uri = "/wg/dubbo/fun", httpCommandType = "GET", auth = 0)
    public ReqDto fun() {
        ReqDto xReq = new ReqDto();
        xReq.setName("piggy");
        xReq.setUid(6666);
        return xReq;
    }

    @Override
    @ApiProducerMethod(methodType = "DUBBO", uri = "/wg/dubbo/test", httpCommandType = "GET", auth = 0)
    public String test() {
        return "@test";
    }

    @Override
    @ApiProducerMethod(methodType = "DUBBO", uri = "/wg/dubbo/funString", httpCommandType = "GET", auth = 0,role = "user")
    public String funString(String s) {
        return "String --> "+s+" port --> "+port;
    }

    @Override
    @ApiProducerMethod(methodType = "DUBBO", uri = "/wg/dubbo/funFloat", httpCommandType = "GET", auth = 0)
    public String funFloat(Float f) {
        return "Float --> "+f+" port --> "+port;
    }

    @Override
    @ApiProducerMethod(methodType = "DUBBO", uri = "/wg/dubbo/funObject", httpCommandType = "POST", auth = 0)
    public String funObject(ReqDto req) {
        return "XReq --> "+req.toString()+" port --> "+port;
    }

    @Override
    @ApiProducerMethod(methodType = "DUBBO", uri = "/wg/dubbo/upload/many", httpCommandType = "POST", auth = 0)
    public String uploadMany(FilesDto file) {
        List<FileDto> filesDto = file.getFilesDto();
        for(FileDto f: filesDto){
            getFile(f.getBytes(),f.getFilename());
        }
        return "success";
    }

    @Override
    @ApiProducerMethod(methodType = "DUBBO", uri = "/wg/dubbo/upload/one", httpCommandType = "POST", auth = 0)
    public String uploadOne(OneFileDto file) {
        FileDto f = file.getFile();
        getFile(f.getBytes(),f.getFilename());
        return "success";
    }

    public void getFile(byte[] bfile,String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        Random random = new Random();
        int i = random.nextInt(3);
        String filePath = "C:\\Users\\smart\\Pictures\\Saved Pictures\\"+i;
        try {
            File dir = new File(filePath);
            //判断文件目录是否存在
            if(!dir.exists()){
                boolean mkdirs = dir.mkdirs();
            }
            String newName = UUID.randomUUID()+"."+fileName.split("\\.")[1];
            File file = new File(filePath+"\\"+ newName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
